﻿using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZhaoxiLearningWpf.Common;
using ZhaoxiLearningWpf.Model;

namespace ZhaoxiLearningWpf.ViewModel
{
    public class FirstPageViewModel : NotifyBase
    {

        private double _instrumentValue;
        public double InstrumentValue {
            get {
                return _instrumentValue;
            }
            set {
                _instrumentValue = value;
                this.DoNotify();
            }
        }


        public ObservableCollection<SeriseCoursesModel> SeriseCourses { get; set; } = new ObservableCollection<SeriseCoursesModel>();

        public FirstPageViewModel()
        {
            this.RefreshInsturmentValue();

            this.InitSeriseCourse();
        }

        private void InitSeriseCourse()
        {
            this.SeriseCourses.Add(
            new SeriseCoursesModel()
            {
                CourseName = "傻宝宝",
                SeriesNames = new LiveCharts.SeriesCollection()
                {
                    new PieSeries(){
                        Title="摸摸",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(60) },
                        DataLabels = false
                    },
                    new PieSeries(){
                        Title="瓜宝",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(40) },
                        DataLabels = false
                    },
                    new PieSeries(){
                        Title="芬迪",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(30) },
                        DataLabels = false
                    }
                },
                CourseInfos = new ObservableCollection<CourseInfoModel>()
                {
                    new CourseInfoModel()
                    {
                        CouseName = "养猫",
                        CurrentVale = 165,
                        GrowRate = 34,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "喂猫",
                        CurrentVale = 165,
                        GrowRate = 56,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "铲屎",
                        CurrentVale = 165,
                        GrowRate = 75,
                        IsGrowing = false
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "洗碗",
                        CurrentVale = 200,
                        GrowRate = 23,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "逗猫",
                        CurrentVale = 563,
                        GrowRate = 96,
                        IsGrowing = false
                    }
                }
            });
            this.SeriseCourses.Add(
            new SeriseCoursesModel()
            {
                CourseName = "傻宝宝",
                SeriesNames = new LiveCharts.SeriesCollection()
                {
                    new PieSeries(){
                        Title="摸摸",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(60) },
                        DataLabels = false
                    },
                    new PieSeries(){
                        Title="瓜宝",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(40) },
                        DataLabels = false
                    },
                    new PieSeries(){
                        Title="芬迪",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(30) },
                        DataLabels = false
                    }
                },
                CourseInfos = new ObservableCollection<CourseInfoModel>()
                {
                    new CourseInfoModel()
                    {
                        CouseName = "养猫",
                        CurrentVale = 165,
                        GrowRate = 34,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "喂猫",
                        CurrentVale = 165,
                        GrowRate = 56,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "铲屎",
                        CurrentVale = 165,
                        GrowRate = 75,
                        IsGrowing = false
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "洗碗",
                        CurrentVale = 200,
                        GrowRate = 23,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "逗猫",
                        CurrentVale = 563,
                        GrowRate = 96,
                        IsGrowing = false
                    }
                }
            });
            this.SeriseCourses.Add(
            new SeriseCoursesModel()
            {
                CourseName = "傻宝宝",
                SeriesNames = new LiveCharts.SeriesCollection()
                {
                    new PieSeries(){
                        Title="摸摸",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(60) },
                        DataLabels = false
                    },
                    new PieSeries(){
                        Title="瓜宝",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(40) },
                        DataLabels = false
                    },
                    new PieSeries(){
                        Title="芬迪",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(30) },
                        DataLabels = false
                    }
                },
                CourseInfos = new ObservableCollection<CourseInfoModel>()
                {
                    new CourseInfoModel()
                    {
                        CouseName = "养猫",
                        CurrentVale = 165,
                        GrowRate = 34,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "喂猫",
                        CurrentVale = 165,
                        GrowRate = 56,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "铲屎",
                        CurrentVale = 165,
                        GrowRate = 75,
                        IsGrowing = false
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "洗碗",
                        CurrentVale = 200,
                        GrowRate = 23,
                        IsGrowing = true
                    },
                    new CourseInfoModel()
                    {
                        CouseName = "逗猫",
                        CurrentVale = 563,
                        GrowRate = 96,
                        IsGrowing = false
                    }
                }
            });
        }

        ~FirstPageViewModel()
        {
            taskSiwtch = false;
            Task.WaitAll(tList.ToArray());
        }

        volatile bool taskSiwtch = true;
        Random rm = new Random();
        List<Task> tList = new List<Task>();
        public void RefreshInsturmentValue()
        {
            var task = Task.Run(() => {
                while (taskSiwtch) {
                    this.InstrumentValue = rm.NextDouble() * 120 - 20;

                    Thread.Sleep(1000);
                }
            });
            tList.Add(task);
        }


    }
}
